namespace jQube.Web.UI.Controls
{
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    //[ToolboxBitmap(typeof(AccordionPane), "Accordion.Accordion.ico")]

    [ParseChildren(true)]
    [PersistChildren(false)]
    [ToolboxData("<{0}:AccordionPane runat=\"server\"></{0}:AccordionPane>")]
    public class AccordionPane : WebControl
    {
        private ITemplate headerTemplate;
        private ITemplate contentTemplate;

        private AccordionHeaderPanel header;
        private AccordionContentPanel content;

        public AccordionPane()
            : base(HtmlTextWriterTag.Div)
        {
            this.CssClass = "jQube-accordion-pane";
        }

        [Browsable(false)]
        [DefaultValue(null)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(AccordionHeaderPanel))]
        [TemplateInstance(TemplateInstance.Single)]
        public virtual ITemplate Header
        {
            get { return this.headerTemplate; }
            set { this.headerTemplate = value; }
        }

        [Browsable(false)]
        [DefaultValue(null)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(AccordionContentPanel))]
        [TemplateInstance(TemplateInstance.Single)]
        public virtual ITemplate Content
        {
            get { return this.contentTemplate; }
            set { this.contentTemplate = value; }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public AccordionHeaderPanel HeaderContainer
        {
            get
            {
                base.EnsureChildControls();
                return this.header;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public AccordionContentPanel ContentContainer
        {
            get
            {
                base.EnsureChildControls();
                return this.content;
            }
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            this.header = new AccordionHeaderPanel(this);
            this.Controls.Add(this.header);

            this.content = new AccordionContentPanel(this);
            this.Controls.Add(this.content);

            if (this.headerTemplate != null)
                this.headerTemplate.InstantiateIn(this.header);

            if (this.contentTemplate != null)
                this.contentTemplate.InstantiateIn(this.content);
        }

        [DefaultValue("jQube-accordion-pane")]
        public override string CssClass
        {
            get
            {
                return base.CssClass;
            }
            set
            {
                base.CssClass = value;
            }
        }

        public override Control FindControl(string id)
        {
            base.EnsureChildControls();
            return base.FindControl(id) ?? this.header.FindControl(id) ?? this.content.FindControl(id);
        }

        protected override void OnInit(System.EventArgs e)
        {
            this.EnsureChildControls();
            base.OnInit(e);
        }
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            base.RenderBeginTag(writer);
        }
    }
}
